package tpo.lmhconsumer.lmhconsumer.controller;

import com.alibaba.fastjson.JSON;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import dto.Dto;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import pojo.Sort;
import tpo.lmhconsumer.lmhconsumer.service.BrandService;
import until.FtpUtil;

import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * 品牌
 */
@RestController
public class BrandController {
    @Autowired
    BrandService brandService;
    /**
     * @param map
     * @return
     */
    @RequestMapping("getBrand")
    @HystrixCommand(fallbackMethod = "rd_map")
    Dto getBrand(@RequestBody Map map){
        return brandService.getBrand(map);
    }

    /**
     * @param map
     * @return
     */
    @RequestMapping("delBrand")
    @HystrixCommand(fallbackMethod = "rd_map")
    Dto delBrand(@RequestBody Map map){
        return brandService.delBrand(map);
    }


    @RequestMapping("addBrand")
    Dto addBrand(@RequestParam(value = "file", required = false) MultipartFile file,
                 @RequestParam(value = "json", required = false) String json){
        Map map= JSON.parseObject(json, Map.class);
        if (file!=null){
            FtpUtil ftpUtil=new FtpUtil();
            String oldername=file.getOriginalFilename();
            String suffix= FilenameUtils.getExtension(oldername);
            String newname= UUID.randomUUID().toString().replace("-", "").substring(15).toLowerCase()+ "."+suffix;
            System.out.println(newname);
            FtpUtil.FtpConfig ftpConfig=new FtpUtil.FtpConfig();
            ftpConfig.setIp("192.168.2.80");
            ftpConfig.setPort(21);
            ftpConfig.setUserName("ftpuser");
            ftpConfig.setPassword("123456");
            try {
                ftpUtil.upload(ftpConfig,
                        "/tpo/",
                        file.getInputStream(),
                        newname);
            } catch (IOException e) {
                e.printStackTrace();
            }
            map.put("log",newname);
        }
        Dto dto=brandService.addBrand(map);
        return dto;
    }

    @RequestMapping("updateBrand")
    Dto updateBrand(@RequestParam(value = "file", required = false) MultipartFile file,
                    @RequestParam(value = "json", required = false) String json){
        Map map= JSON.parseObject(json, Map.class);
        if (file!=null){
            FtpUtil ftpUtil=new FtpUtil();
            String oldername=file.getOriginalFilename();
            String suffix= FilenameUtils.getExtension(oldername);
            String newname= UUID.randomUUID().toString().replace("-", "").substring(15).toLowerCase()+ "."+suffix;
            System.out.println(newname);
            FtpUtil.FtpConfig ftpConfig=new FtpUtil.FtpConfig();
            ftpConfig.setIp("192.168.2.80");
            ftpConfig.setPort(21);
            ftpConfig.setUserName("ftpuser");
            ftpConfig.setPassword("123456");
            try {
                if (map.get("log")!=null)
                ftpUtil.deleteFile(ftpConfig,"/tpo/",map.get("log").toString());
                ftpUtil.upload(ftpConfig, "/tpo/", file.getInputStream(), newname);
            } catch (IOException e) {
                e.printStackTrace();
            }
            map.put("log",newname);
        }
        Dto dto=brandService.updateBrand(map);
        return dto;
    }


    private Dto rd_map(Map map){
        Dto dto=new Dto();
        dto.setMsg("熔断");
        return dto;
    }
}